package stattracker;

import java.io.*;
import java.util.Scanner;

import javax.swing.SwingWorker;

public class FriendsListParser 
{
	BaseSystem sys;
	String file;
	public FriendsListParser(BaseSystem system, String fileName)
	{
		sys = system;
		file = fileName;
		parseFriends();
	}
	
	private void parseFriends()
	{
		try
		{
			Scanner scr = new Scanner(new File(file));
			String line;
			do
			{
				line = scr.nextLine();
				if(line.contains("<h3>"))
					process(line);
			}
			while(scr.hasNextLine());
		}
		catch(FileNotFoundException e){}
	}
	
	private void process(String line)
	{
		int start = line.indexOf(">");
		int end = line.indexOf("<", start);
		final String tag = line.substring(start+1, end);
		SwingWorker<Integer, Void> worker = new SwingWorker<Integer, Void>(){
			public Integer doInBackground()
			{
				sys.addWorker();
				sys.addGamer(tag);
				sys.workerDone();
				return 1;
			}
		};
		worker.execute();
	}
}
